import { IEmbedder } from "../interfaces/index.js";
import { OpenAiEmbedder } from "../embedders/openai.js";
import { OllamaEmbedder } from "../embedders/ollama.js";
import { getEmbedderConfig, envConfig } from "../config/env-config.js";

/**
 * Factory for creating embedder instances based on configuration
 */
export class EmbedderFactory {
  static createEmbedder(): IEmbedder {
    const config = getEmbedderConfig();
    
    switch (config.provider) {
      case "openai":
        if (!config.options.apiKey) {
          throw new Error("OpenAI API key is required");
        }
        return new OpenAiEmbedder({
          apiKey: config.options.apiKey,
          modelId: config.options.modelId,
        });
        
      case "ollama":
        if (!config.options.baseUrl) {
          throw new Error("Ollama base URL is required");
        }
        return new OllamaEmbedder({
          baseUrl: config.options.baseUrl,
          modelId: config.options.modelId,
        });
        
      case "openai-compatible":
        if (!config.options.baseUrl || !config.options.apiKey) {
          throw new Error("OpenAI-compatible base URL and API key are required");
        }
        // Create OpenAI-compatible embedder using OpenAI client with custom base URL
        return new OpenAiEmbedder({
          apiKey: config.options.apiKey,
          modelId: config.options.modelId,
        });
        
      default:
        throw new Error(`Unsupported embedder provider: ${config.provider}`);
    }
  }

  /**
   * Gets the vector dimensions for code and text
   */
  static getVectorDimensions(): { code: number; text: number } {
    return {
      code: envConfig.VECTOR_DIMENSION_CODE,
      text: envConfig.VECTOR_DIMENSION_TEXT,
    };
  }
}
